package 模板方法模式;

/**
 * 模板方法:
 *      多个类中有相同的代码，将多个类中通用的逻辑抽取到一个父类中去

 */
public class TemplateMethodPattern {


    public static void main(String[] args) {
        DiscountCalculator1 discountCalculator1 = new DiscountCalculator1();
        discountCalculator1.calculate();

        DiscountCalculator2 discountCalculator2 = new DiscountCalculator2();
        discountCalculator2.calculate();

        DiscountCalculator3 discountCalculator3 = new DiscountCalculator3();
        discountCalculator3.calculate();
    }
    /**
     * 通用方法接口
     */
    public interface  DiscountCalculator {
        void calculate();
    }

    /**
     * 模板方法 模板
     */
    public static abstract class AbastractDiscountCalculator implements DiscountCalculator {

        public void calculate() {
            commonCalculator();
            specialCalculator();
        }

        private void commonCalculator() {
            System.out.println("通用计算逻辑");
        }

        protected abstract void specialCalculator();
    }

    public static class DiscountCalculator1 extends AbastractDiscountCalculator {

        @Override
        protected void specialCalculator() {
            System.out.println("计算器1 特殊逻辑1");
        }
    }

    public static class DiscountCalculator2 extends AbastractDiscountCalculator {

        @Override
        protected void specialCalculator() {
            System.out.println("计算器2 特殊逻辑2");
        }
    }

    public static class DiscountCalculator3 extends AbastractDiscountCalculator {

        @Override
        protected void specialCalculator() {
            System.out.println("计算器3 特殊逻辑3");
        }
    }
}
